using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

//svnkit imports
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
	/// <summary>
	/// SVN Checkout
	/// </summary>
	public class SVNpropset : CommandBase
	{

		/// <summary>
		/// We have our variables declared here.
		/// Access modifiers as you can observe, is public. Why?
		/// Because we trust our programmers.
		/// 
		/// Try to be verbose with the variable names.
		/// Also, try to include *sane defaults*. Take peg_revision for eg.
		/// </summary>
	
		public java.io.File destination_path;
		public string propName;
		public SVNPropertyValue propValue;
		public SVNRevision revision;
		public bool force;
		public bool recursive;
		public ISVNPropertyHandler handler;
		
		
		/// <summary>
		/// Full constructor for Checkout.
		/// </summary>
		/// <param name="dest_path">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="allow_unv_obs">
		/// A <see cref="System.Boolean"/>
		/// </param>
		public void propset( string dest_path, string name, string values) {
			this.destination_path = new java.io.File(dest_path);
			this.propName = name;
			this.revision = SVNRevision.HEAD;
			this.propValue = SVNPropertyValue.create(values);
			this.force = true;
			this.recursive = true;
			this.handler = null;
		}
		
/*		public SVNpropset( string dest_path, string name, SVNRevision revis,
		                   string values, bool forced,
		                   bool allow_unv_obs, ISVNPropertyHandler handling) 
		{
			//DEBUG MSG. *Always* include this.
			Console.WriteLine("SVN Switch normal Constructor");
			
			this.destination_path = new java.io.File(dest_path);
			this.propName = name;
			this.revision = revis;
			this.propValue = SVNPropertyValue.create(values);
			this.force = forced;
			this.recursive = allow_unv_obs;
			this.handler = handling;
		}*/
		/// <summary>
		/// Execute SVN Checkout
		/// </summary>
		public override void Execute ()
		{
			//DEBUG MSG. *Always* include this.
			Console.WriteLine("SVN propset Execution");
			
			//library setup
			Tools.setupLibrary();
			
			//load up the clients
			SVNClientManager cm = SVNClientManager.newInstance();
			SVNWCClient wc = cm.getWCClient();			
			
			try {
				wc.doSetRevisionProperty (this.destination_path, this.revision, this.propName, 
				              this.propValue, this.force, this.handler);							
			}
			catch (SVNException e) {
				//we convert this exception to a non SVNKit exception.
				//we want to minimize interfacing with SVNkit outside of this namespace (abstraction)
				throw new SVNExecuteException(e.getMessage());
			}
		}
		public override Dictionary<string, List<object>> getTokens ()
		{
			Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
			ret.Add("propset",new List<object>() {"ps","propset"});
			return ret;
		}
		
		public override List<KeyValuePair<string,initPointer>> getInitPointers() {
			List<KeyValuePair<string,initPointer>> ret = new List<KeyValuePair<string,initPointer>>();
			ret.Add(new KeyValuePair<string,initPointer>("propset",new initPointer(this.handleCLI)));
			return ret;
		}
		
		public void handleCLI(Dictionary<string,List<object>> tokens) {
			if (tokens["propset"].Count != 3) {
				Console.WriteLine("Not enough arguments: propset requires at least 3 argument");;
				return;
			}
			string location = (string)tokens["propset"][0];
			Console.WriteLine(location);
			string propertyname = (string)tokens["propset"][1];
			Console.WriteLine(propertyname);
			string propertyvalue = (string)tokens["propset"][2]; 
			Console.WriteLine(propertyvalue);
			this.propset(location, propertyname, propertyvalue);
			this.Execute();
		}

	}
}
